﻿using Db4objects.Db4o;
using Db4objects.Db4o.CS;
using Db4objects.Db4o.CS.Config;
using Db4objects.Db4o.CS.Internal.Config;
using Db4objects.Db4o.Config;
using Db4objects.Db4o.TA;
using ZOIL.DataModel;

namespace ZOIL.DataBackend.Server
{
    /// <summary>
    /// The Basic DB4O Configuration that must be applied to both client and server to work
    /// </summary>
    public class Db4oConfig
    {
        /// <summary>
        /// Applies the config to the global configuration scope
        /// </summary>
        public static void ApplyConfig()
        {
            //we need transparent persistence support
            Db4oFactory.Configure().Add(new TransparentPersistenceSupport());

            //turn on Indexing and Cascading for ZInformationObjects
            Db4oFactory.Configure().ObjectClass(typeof(IPersistable)).Indexed(true);
            Db4oFactory.Configure().ObjectClass(typeof(IPersistable)).CascadeOnUpdate(true);
            Db4oFactory.Configure().ObjectClass(typeof(IPersistable)).CascadeOnActivate(true);
            Db4oFactory.Configure().ObjectClass(typeof(IPersistable)).CascadeOnDelete(true);
            Db4oFactory.Configure().LockDatabaseFile(false);

            //            Db4oFactory.Configure().MarkTransient("DataBackend.TransientProperty");

            //generate UUIDS for all objects
            Db4oFactory.Configure().GenerateUUIDs(ConfigScope.Globally);
        }

        public static IServerConfiguration GetServerConfiguration()
        {
            IServerConfiguration config = Db4oClientServer.NewServerConfiguration();

            var impl = (NetworkingConfigurationImpl)config.Networking;
            impl.Config().GenerateUUIDs(ConfigScope.Globally);

            config.File.GenerateUUIDs = ConfigScope.Globally;
            config.File.LockDatabaseFile = false;


            SetCommonConfig(config.Common);

            return config;
        }

        public static IClientConfiguration GetClientConfiguration()
        {
            IClientConfiguration config = Db4oClientServer.NewClientConfiguration();

            var impl = (NetworkingConfigurationImpl)config.Networking;
            impl.Config().GenerateUUIDs(ConfigScope.Globally);

            SetCommonConfig(config.Common);

            return config;
        }

        private static void SetCommonConfig(ICommonConfiguration config)
        {
            config.Add(new TransparentPersistenceSupport());
            //config.CallConstructors = true;
            config.ObjectClass(typeof(IPersistable)).Indexed(true);
            config.ObjectClass(typeof(IPersistable)).CascadeOnUpdate(true);
            config.ObjectClass(typeof(IPersistable)).CascadeOnActivate(true);
            config.ObjectClass(typeof(IPersistable)).CascadeOnDelete(true);
            config.WeakReferences = false;
            //config.MarkTransient("ZOIL.DataBackend.NotPersisted");
            //config.ObjectClass("System.Windows.Media.PathGeometry, PresentationCore").Translate(new TTransient());
            //config.ObjectClass("System.Windows.TriggerCollection, PresentationFramework").Translate(new TTransient());
            //config.ObjectClass("System.Windows.Markup.ParserStack, PresentationFramework").Translate(new TTransient());
            //config.ObjectClass("System.Windows.Media.VisualCollection, PresentationCore").Translate(new TTransient());

            //config.RegisterTypeHandler(new TypeHandlerPredicate(),new IgnoreFieldsTypeHandler());


            Db4oFactory.Configure().GenerateUUIDs(ConfigScope.Globally);
        }
    }

}